package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.AddressbookdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.AddressdbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by zhongsheng-luojianlan on 16/11/23.
 */

public class AddressService {
    //插入或者更新数据
    public static boolean saveOrUpdateAddressdbMode(AddressdbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            model.setTime(System.currentTimeMillis());
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteById(String addressid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(AddressdbModel.class,addressid);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取列表
    public static List<AddressdbModel> getAddressList(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            List<AddressdbModel> list = db.selector(AddressdbModel.class).where("userid","==",userid).orderBy("time",true).findAll();
            return list;
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }
}
